package org.aurora.four.atomic_7;

import java.lang.management.ThreadMXBean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author : Aurora
 * @date : 2024/3/19 17:28
 * @description AtomicLong实战
 */
public class AtomicLongAnalysis {
    static Integer[] arrayOne = new Integer[]{0,1,0,3,2,0,6,8,3,8,2,1,8};//3
    static Integer[] arrayTwo = new Integer[]{0,1,0,3,0,0,6,8,0,8,2,1,0};//6

    public static Long count(Integer[] arr1, Integer[] arr2) throws InterruptedException {
        AtomicLong countZero = new AtomicLong(0);
        Thread thread1 = new Thread(()->{
            for (Integer index:arr1){
                if(index == 0){
                    countZero.incrementAndGet();
                }
            }
        });
        Thread thread2 = new Thread(()->{
            for (Integer index:arr2){
                if(index == 0){
                    countZero.incrementAndGet();
                }
            }
        });
        thread1.start();
        thread2.start();
        //join不能省，否则会导致结果不正确
        thread1.join();
        thread2.join();
        return countZero.get();
    }
    public static void main(String[] args) throws InterruptedException {
        Long count = count(arrayOne, arrayTwo);
        System.out.println("0的个数"+count);
    }
}
